package com.wencai.util;

import com.wencai.bean.Duizi;
import com.wencai.bean.Stock;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author wangpan
 * @date 2023/4/10
 */
public class ExportUtils {

    private static List<List<String>> getHead() {
        List<List<String>> list = new ArrayList<>();
        list.add(Collections.singletonList("名称"));
        list.add(Collections.singletonList("最低价"));
        list.add(Collections.singletonList("最低价出现时间"));
        list.add(Collections.singletonList("300日内涨停次数"));
        list.add(Collections.singletonList("行业"));
        list.add(Collections.singletonList("流通市值(亿元)"));
        return list;
    }

    private static List<List<String>> getIndexHead() {
        List<List<String>> list = new ArrayList<>();
        list.add(Collections.singletonList("指数名称"));
        list.add(Collections.singletonList("大涨次数"));
        list.add(Collections.singletonList("最高涨幅"));
        list.add(Collections.singletonList("第一次大涨日期"));
        list.add(Collections.singletonList("第一次大涨涨幅"));
        list.add(Collections.singletonList("第二次大涨日期"));
        list.add(Collections.singletonList("第二次大涨涨幅"));
        list.add(Collections.singletonList("最后一次大涨日期"));
        list.add(Collections.singletonList("最后一次大涨涨幅"));
        return list;
    }

    public static void export(List list0, List list00, List list1, List list2, List list3, List list4, List list5, List list6, Class clazz, String outFilePath) throws IOException {
        List<List<String>> head = getHead();
        ExcelWriter excelWriter = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
        try {
            excelWriter = EasyExcel.write(bos, clazz).build();
            excelWriter.write(list0, EasyExcel.writerSheet("300日内涨停次数大于10的")
                    .head(head)
                    .build());
            excelWriter.write(list00, EasyExcel.writerSheet("300日内涨停次数大于5小于10的")
                    .head(head)
                    .build());
            excelWriter.write(list1, EasyExcel.writerSheet("AB.AB")
                    .head(head)
                    .build());
            excelWriter.write(list2, EasyExcel.writerSheet("A.BB")
                    .head(head)
                    .build());
            excelWriter.write(list4, EasyExcel.writerSheet("A.A")
                    .head(head)
                    .build());
            excelWriter.write(list5, EasyExcel.writerSheet("所有数据")
                    .head(head)
                    .build());
            excelWriter.write(list6, EasyExcel.writerSheet("所有数据(不包括整数价格)")
                    .head(head)
                    .build());
        } finally {
            bos.flush();
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }


    public static void exportDaily(List list100, List list20, List list10, List list5, Class clazz, String outFilePath) throws IOException {
        List<List<String>> head = getHead();
        ExcelWriter excelWriter = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
        try {
            excelWriter = EasyExcel.write(bos, clazz).build();
            List<Duizi> all = new ArrayList<>();
            all.addAll(list100);
            all.addAll(list20);
            all.addAll(list10);
            all.addAll(list5);
            excelWriter.write(all, EasyExcel.writerSheet("总计")
                    .head(head)
                    .build());
            excelWriter.write(list100, EasyExcel.writerSheet("百日")
                    .head(head)
                    .build());
            excelWriter.write(list20, EasyExcel.writerSheet("二十日")
                    .head(head)
                    .build());
            excelWriter.write(list10, EasyExcel.writerSheet("十日")
                    .head(head)
                    .build());
            excelWriter.write(list5, EasyExcel.writerSheet("五日")
                    .head(head)
                    .build());
        } finally {
            bos.flush();
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }


    public static void exportIncrement(List list, Class clazz, String outFilePath) throws IOException {
        List<List<String>> head = getIndexHead();
        ExcelWriter excelWriter = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
        try {
            excelWriter = EasyExcel.write(bos, clazz).build();
            excelWriter.write(list, EasyExcel.writerSheet("总计")
                    .head(head)
                    .build());
        } finally {
            bos.flush();
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    public static void exportIndexStocks(List list, Class clazz, String outFilePath) throws IOException {
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("日期"));
        head.add(Collections.singletonList("指数名称"));
        head.add(Collections.singletonList("序号"));
        head.add(Collections.singletonList("指数涨幅"));
        head.add(Collections.singletonList("个股1名称"));
        head.add(Collections.singletonList("个股1区间涨幅"));
        head.add(Collections.singletonList("个股2名称"));
        head.add(Collections.singletonList("个股2区间涨幅"));
        head.add(Collections.singletonList("个股3名称"));
        head.add(Collections.singletonList("个股3区间涨幅"));
        ExcelWriter excelWriter = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
        try {
            excelWriter = EasyExcel.write(bos, clazz).build();
            excelWriter.write(list, EasyExcel.writerSheet("总计")
                    .head(head)
                    .build());
        } finally {
            bos.flush();
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }


    public static void exportCalculateResult(List list, Class clazz, String outFilePath) throws IOException {
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("买入时间点"));
        head.add(Collections.singletonList("卖出时间点"));
        head.add(Collections.singletonList("总收益"));
        ExcelWriter excelWriter = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
        try {
            excelWriter = EasyExcel.write(bos, clazz).build();
            excelWriter.write(list, EasyExcel.writerSheet("总计")
                    .head(head)
                    .build());
        } finally {
            bos.flush();
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    public static void exportBuyStock(List list, Class clazz, String outFilePath) throws IOException {
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("证券名称"));
        head.add(Collections.singletonList("买入日期"));
        head.add(Collections.singletonList("卖出日期"));
        head.add(Collections.singletonList("买入价格"));
        head.add(Collections.singletonList("卖出价格"));
        head.add(Collections.singletonList("收益"));

        head.add(Collections.singletonList("是否涨停"));
        head.add(Collections.singletonList("最早封板时间"));
        head.add(Collections.singletonList("最后未封涨停时间"));
        head.add(Collections.singletonList("是否中途开板"));
        head.add(Collections.singletonList("买入前的总金额"));
        head.add(Collections.singletonList("卖出后的总金额"));
        head.add(Collections.singletonList("买入消耗金额"));
        head.add(Collections.singletonList("卖出回收金额"));
        head.add(Collections.singletonList("交易手续费"));
        head.add(Collections.singletonList("交易手数"));
        head.add(Collections.singletonList("获利金额"));
        ExcelWriter excelWriter = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
        try {
            excelWriter = EasyExcel.write(bos, clazz).build();
            excelWriter.write(list, EasyExcel.writerSheet("总计")
                    .head(head)
                    .build());
        } finally {
            bos.flush();
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    public static void export(List<List<String>> head, List list, Class clazz, String outFilePath) throws IOException {
        ExcelWriter excelWriter = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
        try {
            excelWriter = EasyExcel.write(bos, clazz).build();
            excelWriter.write(list, EasyExcel.writerSheet("总计")
                    .head(head)
                    .build());
        } finally {
            bos.flush();
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    public static void exportWithSheets(List<List<String>> head, List list, Class clazz, String outFilePath) throws IOException {
        ExcelWriter excelWriter = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
        try {
            int i = 0;
            excelWriter = EasyExcel.write(bos, clazz).build();
            for (Object l : list) {
                List<Stock> s = (List<Stock>) l;
                String sheetNo = "" + i;
                if (s.size() > 0) {
                    sheetNo = s.get(0).getDate();
                }
                excelWriter.write((List) l, EasyExcel.writerSheet(sheetNo)
                        .head(head)
                        .build());
            }
        } finally {
            bos.flush();
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

}
